// import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'model/response_data.dart';

/// 全局错误处理拦截器
class ErrorInterceptor extends Interceptor {
  @override
  onError(DioError err, ErrorInterceptorHandler handler) {
    ResponseData? response;
    switch (err.type) {
      case DioErrorType.connectTimeout:
      case DioErrorType.sendTimeout:
      case DioErrorType.receiveTimeout:
        // response = ResponseData(
        //   code: ErrorCode.NETWORK_TIMEOUT.code,
        //   msg: ErrorCode.NETWORK_TIMEOUT.msg,
        // );
        break;
      case DioErrorType.response:

        /// 在非200-400状态码间,如果后端有返回错误码或错误信息,则使用后端错误码信息
        dynamic errData = err.response!.data;
        if (errData != null &&
            errData['code'] != null &&
            errData['msg'] != null) {
          // response = ResponseData(
          //   code: errData['code'],
          //   msg: errData['msg'],
          //   data: errData['data'],
          // );
        } else {
          switch (err.response!.statusCode) {
            case 401:
              // response = ResponseData(
              //   code: err.response!.statusCode,
              //   msg: '请先登录',
              //   data: err.response!.data,
              // );
              break;
            case 403:
              // response = ResponseData(
              //   code: err.response!.statusCode,
              //   msg: '没有权限',
              //   data: err.response!.data,
              // );
              break;
            case 404:
              // response = ResponseData(
              //   code: err.response!.statusCode,
              //   msg: '路径不存在',
              //   data: err.response!.data,
              // );
              break;
            case 500:
            case 501:
            case 502:
            case 503:
            case 504:
              // response = ResponseData(
              //   code: ErrorCode.SERVER_ERROR.code,
              //   msg: _isDebug ? err.response!.data : ErrorCode.SERVER_ERROR.msg,
              //   data: err.response!.data,
              // );
              break;
            default:
              // response = ResponseData(
              //   code: err.response!.statusCode,
              //   msg: err.response!.statusMessage,
              //   data: err.response!.data,
              // );
              break;
          }
        }
        break;
      case DioErrorType.cancel:
        // response = ResponseData(
        //   code: ErrorCode.NETWORK_CONNECT_CANCEL.code,
        //   msg: ErrorCode.NETWORK_CONNECT_CANCEL.msg,
        // );
        break;
      case DioErrorType.other:
        if (err.error != null && err.error is HttpException) {
          // HttpException e = err.error as HttpException;
          // response = ResponseData(
          //   code: e.code,
          //   msg: e.msg,
          //   data: e.response?.data?.data ?? err.response?.data,
          // );
          // err.response = e.response;
          // if (ErrorCode.TOKEN_INVALID.code == response.code ||
          //     ErrorCode.RE_LOGIN.code == response.code) {
          //   SchedulerBinding.instance.addPostFrameCallback((_) {
          //     SpUtil.remove(SPKeys.TOKEN_KEY);
          //     SpUtil.remove(SPKeys.USER_INFO);
          //     // AppManager.showUnLoginDialog(msg: '登录失效,请重新登录');
          //   });
          // }
        } else if (err.error != null && err.error is SocketException) {
          // /// 网络相关错误
          // SocketException? e = err.error as SocketException?;
          // response = ResponseData(
          //   code: ErrorCode.NETWORK_CONNECT_ERROR.code,
          //   msg: _isDebug
          //       ? e!.osError!.message
          //       : ErrorCode.NETWORK_CONNECT_ERROR.msg,
          // );
        } else {
          // response = ResponseData(
          //   code: ErrorCode.NETWORK_ERROR.code,
          //   msg: _isDebug ? err.error?.toString() : ErrorCode.NETWORK_ERROR.msg,
          //   data: err.response?.data,
          // );
        }
        break;
    }

    /// 统一异常格式
    if (err.response == null) {
      err.response = Response(
        requestOptions: err.requestOptions,
        data: response,
        // 这个值如果不初始化, LogInterceptor里会报错
        redirects: [],
        // 这个值如果不初始化, LogInterceptor里会报错
        headers: Headers(),
      );
    } else {
      err.response!.data = response;
    }
    return handler.next(err);
  }
}
